package com.bicon.life.core.base.config;

import com.bicon.life.core.base.common.Constants;
import com.bicon.life.core.base.common.entity.BaseResult;
import com.bicon.life.core.base.common.utils.HttpUtil;
import com.bicon.life.core.base.repository.redis.RedisHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 系统拦截器
 * 1.通过token获取userId
 * 2.接口加密校验
 *
 * @author tandk
 * @date 2017/3/7 0007
 */
@Slf4j
public class SystemInterceptor implements HandlerInterceptor {

    @Autowired
    RedisHandler redisHandler;

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        log.info("【{}_{}请求_Start】{}", HttpUtil.getRemoteHost(request), request.getMethod(), HttpUtil.getFullUrl(request));
        //1.通过token获取userId
        String url = request.getRequestURL().toString();
        if (url.contains("/api/")) {
            String token = request.getParameter("token");
            String userId = redisHandler.getGlobalRedisTemplate().getStringValue(Constants.BOTU_USER_TOKEN, token);
            if (StringUtils.isEmpty(userId)) {
                HttpUtil.printFailMessage(response, new BaseResult(Constants.RESPONSE_CODE_300, Constants.RESPONSE_MESSAGE_300));
                return false;
            }
            request.setAttribute("userId", userId);
        }

        //2.接口加密校验
//        String saltVal = map.get("key").toString();
//        if(!MD5Util.sortAndMd5ForParams(saltVal,request)){
//            HttpUtil.printFailMessage(response, new BaseResult(MerchantConstants.RESPONSE_CODE_401, MerchantConstants.RESPONSE_MESSAGE_401));
//            return false;
//        }
        return true;
    }

//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
//        log.info("【{}_{}请求_Start】{}", HttpUtil.getRemoteHost(request), request.getMethod(), HttpUtil.getFullUrl(request));
//        //1.通过token获取userId
//        String url = request.getRequestURL().toString();
//        if (url.contains("/api/")) {
//            String token = request.getParameter("token");
//            String userIdAndDate = redisHandler.getGlobalRedisTemplate().getStringValue(Constants.LIFE_USER_TOKEN, token);
//            if (StringUtils.isEmpty(userIdAndDate)) {
//                HttpUtil.printFailMessage(response, new BaseResult(Constants.RESPONSE_CODE_300, Constants.RESPONSE_MESSAGE_300));
//                return false;
//            }
//            String uuId = request.getParameter("uuId");
//            String[] flag = userIdAndDate.split("#");
//            if (flag != null && flag.length > 1) {
//                //              对比设备号是否一致
//                if (!uuId.equals(flag[2])) {
//                    HttpUtil.printFailMessage(response, new BaseResult(Constants.RESPONSE_CODE_300, Constants.RESPONSE_MESSAGE_300));
//                    return false;
//                }
//                if (LocalDateTime.now().compareTo(LocalDateTime.parse(flag[1]).plusMinutes(Constants.TOKEN_EXPIRE_TIME)) >= 0) {
//                    redisHandler.getGlobalRedisTemplate().cacheStringValue(Constants.LIFE_USER_TOKEN, token, flag[0] + "#" + LocalDateTime.now() + "#" + uuId, Constants.TOKEN_TIME_OUT);
//                }
//                request.setAttribute("userId", flag[0]);
//            } else {
//                request.setAttribute("userId", userIdAndDate);
//            }
//        }
//        return true;
//    }

    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
